package com.fjec.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.DateUtil;
import com.fjec.common.util.SaltMD5;
import com.fjec.user.entity.SysUser;
import com.fjec.user.entity.SysUserApp;
import com.fjec.user.mapper.SysUserMapper;
import com.fjec.user.service.ISysUserAppService;
import com.fjec.user.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 002.用户表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-08-31
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Resource
    private SysUserMapper mapper;

    @Autowired
    private ISysUserAppService sysUserAppService;

    @Override
    public JsonResult saveUser(SysUser sysUser) {
        String password = sysUser.getUserPwd();
        String saltMD5Password;
        try {
            saltMD5Password = SaltMD5.MD5WithSalt(password, 0, null);
        } catch (Exception e) {
            return JsonResult.FAILURE;
        }
        sysUser.setUserPwd(saltMD5Password);
        sysUser.setLocked(0);
        LocalDateTime localDateTime = DateUtil.nowLdt();
        sysUser.setCreateTime(localDateTime);
        sysUser.setUpdateTime(localDateTime);
        boolean saved = this.save(sysUser);
        if (saved) return JsonResult.SUCCESS;
        else return JsonResult.FAILURE;
    }

    @Override
    public JsonResult updateUser(SysUser sysUser) {
        sysUser.setUpdateTime(DateUtil.nowLdt());
        boolean updated = this.updateById(sysUser);
        if (updated) return JsonResult.SUCCESS;
        else return JsonResult.FAILURE;
    }

    @Override
    public SysUser getUserByName(String userName) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("UserName", userName);
        SysUser sysUser = this.getOne(wrapper);
        return sysUser;
    }

    @Override
    public JsonResult getUsersPage(Page page) {
        page = this.page(page);
        return JsonResult.success()
                .data(page.getRecords())
                .total((long) page.getRecords().size())
                .build();
    }

    @Override
    public SysUser getSysUserByUserAppId(String userAppId, Integer appType) {
        return mapper.getSysUserByUserAppId(userAppId, appType);
    }

    @Override
    public int updateUserPhone(Long userId, String phone) {
        SysUser entity = new SysUser();
        entity.setId(userId);
        entity.setPhone(phone);
        return mapper.updateById(entity);
    }

    @Override
    public String getOpenIdByUserId(Long userId) {
        SysUserApp userApp = sysUserAppService.getOne(new QueryWrapper<SysUserApp>().eq("UserId", userId));
        if (userApp == null)
            return null;
        return userApp.getUserAppId();
    }

}
